manager <- c(1,2,3,4,5)
date <- c('10/12/08','10/12/08','10/1/08','10/12/08','5/1/09')
country <- c('US','US','UK','UK','UK')
gender <- c('M','F','F','M','F')
age <- c(32,45,25,39,99)
q1 <- c(5,3,3,3,2)
q2 <- c(4,5,5,3,2)
q3 <- c(5,2,5,4,1)
q4 <- c(5,5,5,NA,2)
q5 <- c(5,5,2,NA,1)

leadership <- data.frame(manager,date,country,gender,age,q1,q2,q3,q4,q5,stringsAsFactors = FALSE)

#将99岁的年龄设置为缺失值
leadership$age[leadership$age == 99] <- NA
#变量【条件】赋值，将在条件为真的情况下对变量进行赋值

leadership$agecat[leadership$age > 75] <-'Elder'
leadership$agecat[leadership$age >= 55 & leadership$age <= 75] <- 'Middle Aged'
leadership$agecat[leadership$age <55] <- 'Young'

#交互式修改数据框信息
fix(leadership)
#编程式修改数据框
names(leadership)[1] <- 'managerID' 

is.na(leadership[,6:10])
#在年龄等于99的值设置为缺失值
leadership$age[leadership$age == 99] <- NA
leaderformat <- '%d/%m/%y'
leadership$date <- as.Date(leadership$date,leaderformat)#将character类型转换为date类型

#排序
newdata <- leadership[order(leadership$age),]
newdata <- leadership[order(-leadership$age),]#倒序
with(leadership,{
  newdata1 <-leadership[order(gender,-age),]
  newdata1
})
leadership$score <- c(1,2,3,4,5)
#根据现有数据库创建新的数据库
score <- leadership[,c(6:10)]
fields <- c('manager','age','country','gender')
managerinfo <- leadership[,fields]
#删除字段
myvars <- names(leadership) %in% c('q3','q4')#names 获取字段
newdata <- leadership[!myvars]
leadership[c(TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE)]
leadership$q3 <- leadership$score <- NULL
#选择记录 rows,cols
leadership[1:3,]
leadership[leadership$gender == 'M' & leadership$age > 30,]
leadership[leadership$gender == "F",]
with(leadership,{
  leadership[gender == 'M' & age >30,]
})

leadership$date <- as.Date(leadership$date,'%m/%d/%y') 
startdate <- as.Date('2009-01-01')
enddate <- as.Date('2009-10-31')
newdata <- leadership[which(leadership$date >= startdate & leadership$date <= enddate),]

#使用subset函数
newdata <- subset(
  leadership,
  age >= 35 | age <24
)

subset(
  leadership,
  gender == 'M' & age >25,
  select=gender:q4#from:to
)

#随机抽样
sample(100,50,0)
mysample <- leadership[sample(
  1:nrow(leadership),
  3,
  replace=FALSE#不放回，TRUE表示有放回
),]
install.packages('sqldf')
library('sqldf')
?sqldf
newfdf <- sqldf("SELECT count(*) FROM leadership",row.names=FALSE)




#create new variables
mydata <- data.frame(
  x1=c(2,2,6,4),
  x2=c(3,4,2,8)
)
#方法1
mydata$sum <- mydata$x1 + mydata$x2
mydata$meanx <-(mydata$x1 +mydata$x2)/2
#方法2
with(mydata,{
  mydata$x1 <- X1+x2
  mydata$x2 <- (x1+x2)/2
})
#方法3
mydata <- transform(mydata,
                    sumx=x1+x2,
                    meanx=(x1+x2)/2)



#缺失值 查看是否有缺失值
is.na()

#对缺失值的处理：
x <- c(1,2,3,4,NA,6,7,8)
y <- sum(x,na.rm = TRUE)#true表示移出缺失值，并使用剩余值进行计算
z <- mean(x,na.rm = TRUE)
#日期值
mdt <- as.Date(
  c('2017-06-22','2004-02-13')
)
#根据设置的日期，读取日期值
strDates <- c('2017/06/22','2004/02/13')
dates <- as.Date(strDates,"%Y/%m/%d")
cnDate <- '2017年10月21'
dates <- as.Date(cnDate,'%Y年%m月%d')
#读取--> 输出
Sys.Date()
format(Sys.Date(),format='%y年%m月')#按照指定格式输出
format(as.Date('2017-06-28'),format='%a')
#时间计算
startdata <- as.Date('20151215','%Y%m%d')
enddata <- as.Date('20151225','%Y%m%d')
days <- enddata - startdata
difftime(Sys.Date(),as.Date('1992-11-15'),units='weeks')
#日期转换为字符
#as.Date  vs.  as.character
#类型转换
a <- c(1,2,3)
a
is.numeric(a)
is.vector(a)
a <- as.character(a)
a
is.numeric(a)
is.vector(a)
is.character(a)


#数学函数

abs(-1)
sqrt(4)#平方根
ceiling(13.33)
trunc(-1.333)
round(1.32656,digits =2) 

#
x <- c(1,2,3,4,5,6,7,8)

n <- length(x)
meanx <- sum(x)/n
x-36#对数值向量、矩阵、或数据框进行的操作和函数会单独应用于每一个独立的值
css <- sum((x-meanx)^2)/(n-1)
sdx <- sqrt(css)
mean(x)
sd(x)
x<- pretty(c(-3,3),30)
y<-dnorm(x)
plot(x,y,
     type='l',
     xlab = 'normal deviate',
     ylab = 'destiny',
     yaxs='i')
pnorm(1.96)

#生成伪随机数
runif(5)
runif(5)
set.seed(1)
runif(5)

#字符处理函数
nchar('123kik')#计算向量中向量中字符串长度
nchar(c('ab','124','ab123')[2])#组合使用
substr('hello world',1,ceiling( nchar('hello world')/2))
#替换
substr(c('12','你哈','32'),0,2) 
a <- '123456'
substr(a,3,6) <- c('4ab')
#正则
grep('^\\w',c('aa','2','1a'),ignore.case=FALSE,fixed=FALSE)
images <- 'd1d21d.png|d12wd1.png|1wdwdf3f.png'
image <- strsplit(images,'\\|')
#others
length(c(1,2,3))
seq(from=10,by=10,to=100)
rep(1:100,100)
cut(c(1,2,3,4),2)
pretty(10,3)
cat(c('hello','wor\nld','ha\tha','bad\b1'))
cat(c('Hello','Bob','\b.\n','\bIsn\'t',toupper('r'),'great?'))
#将函数应用于矩阵和数据框
#在新的文件中
filename <- 'learnfc.r'
file.create(filename)
file.edit(filename)
